﻿using System;
using CredentialManagement;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SoftwareApproach.TestingExtensions;

namespace ProxyFinder.Tests {
    [TestClass]
    public class WindowsCredentialProviderTests {

        [TestInitialize]
        public void TestInitailize() {
            using(CredentialSet set = new CredentialSet(Constants.TARGET_KEY)) {
                if(set.Load().Count > 0) {
                    set.ForEach(cred => cred.Delete());
                }
            }
        }

        [TestMethod]
        public void WindowsCredentialProvider_Create() {
            IWindowsCredentialProvider provider = new WindowsCredentialProvider();
            provider.ShouldNotBeNull();
        }

        [TestMethod]
        public void WindowsCredentialProvider_GetCredentials_ShouldThrowException_OnNullTarget() {
            IWindowsCredentialProvider provider = new WindowsCredentialProvider();
            Testing.ShouldThrowException<ArgumentNullException>(() => provider.GetCredentials(null));
        }

        [TestMethod]
        public void WindowsCredentialProvider_GetCredentials_ShouldReturnEmptyList_ForNonExistingTarget() {
            IWindowsCredentialProvider provider = new WindowsCredentialProvider();
            provider.GetCredentials(Constants.TARGET_KEY).ShouldBeEmpty();
        }

        [TestMethod]
        public void WindowsCredentialProvider_GetCredentials_ShouldReturValidCredentials_OnSavedTarget() {
            IWindowsCredentialProvider provider = new WindowsCredentialProvider();
            Credential cred = new Credential("username", "password", Constants.TARGET_KEY);
            cred.Save();
            provider.GetCredentials(Constants.TARGET_KEY)
                .ShouldNotBeEmpty();
            cred.Dispose();
        }

        [TestMethod]
        public void WindowsCredentialProvider_PromptForCredentials_ShouldReturnCredentials_ForNewTargetNoForcePrompt() {
            IWindowsCredentialProvider provider = new WindowsCredentialProvider();
            Credential cred = new Credential("username", "password", "INVALID_TARGET");
            cred.Save();

            provider.PromptForCredentials("INVALID_TARGET", false).ShouldNotBeNull();
            cred.Dispose();
        }

        [TestMethod]
        public void WindowsCredentialProvider_PromptForCredentials_ShouldReturnCredentials_ForNewTargetForcePrompt() {
            IWindowsCredentialProvider provider = new WindowsCredentialProvider();
            Credential cred = new Credential("username", "password", "INVALID_TARGET");
            cred.Save();

            provider.PromptForCredentials("INVALID_TARGET", true).ShouldNotBeNull();
            cred.Dispose();
        }

        [TestMethod]
        public void WindowsCredentialProvider_PromptForCredentials_ShouldReturnValidCredentials_OnSavedTarget() {
            IWindowsCredentialProvider provider = new WindowsCredentialProvider();
            Credential cred = new Credential("username", "password", Constants.TARGET_KEY);
            cred.Save();
            provider.PromptForCredentials(Constants.TARGET_KEY, false);
            cred.Dispose();
        }

    }
}
